home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / chap02 / howto08 / ufmgr8.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1996-03-05  |  18.8 KB  |  514 lines

  1. unit Ufmgr8;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, ShellAPI, FileCtrl,
  8.    DRWSUtl1, DRWSUtl2;
  9.  
  10. type
  11.   TCCFileMgrForm = class(TForm)
  12.     Panel1: TPanel;
  13.     Panel2: TPanel;
  14.     BitBtn1: TBitBtn;
  15.     Panel3: TPanel;
  16.     Label1: TLabel;
  17.     BitBtn2: TBitBtn;
  18.     BitBtn3: TBitBtn;
  19.     BitBtn4: TBitBtn;
  20.     BitBtn5: TBitBtn;
  21.     BitBtn6: TBitBtn;
  22.     BitBtn7: TBitBtn;
  23.     OpenDialog1: TOpenDialog;
  24.     BitBtn8: TBitBtn;
  25.     BitBtn9: TBitBtn;
  26.     OpenDialog2: TOpenDialog;
  27.     procedure FormResize(Sender: TObject);
  28.     procedure FormCreate(Sender: TObject);
  29.     procedure BitBtn7Click(Sender: TObject);
  30.     procedure BitBtn1Click(Sender: TObject);
  31.     procedure BitBtn2Click(Sender: TObject);
  32.     procedure BitBtn3Click(Sender: TObject);
  33.     procedure BitBtn4Click(Sender: TObject);
  34.     procedure BitBtn5Click(Sender: TObject);
  35.     procedure DirectoryListBox1Change(Sender: TObject);
  36.     procedure FormPaint(Sender: TObject);
  37.     procedure FileListBox1DblClick(Sender: TObject);
  38.     procedure BitBtn8Click(Sender: TObject);
  39.     procedure BitBtn9Click(Sender: TObject);
  40.     procedure BitBtn6Click(Sender: TObject);
  41.   private
  42.     { Private declarations }
  43.   public
  44.     { Public declarations }
  45.     DirectoryListBox1 : TDirectoryListBox;
  46.     FileListBox1 : TIconFileListBox;
  47.     ScrollBox1 : TFileIconPanelScrollbox;
  48.   end;
  49. var
  50.   CCFileMgrForm      : TCCFileMgrForm;
  51.   CurrentView        : Integer;  { This holds the current view setting      }
  52.  
  53. implementation
  54.  
  55. {$R *.DFM}
  56.  
  57. uses DDDFUnit,  { Include custom directory selection form }
  58.      CfmpfUn;   { Include custom options dialog           }
  59.  
  60. { Set up the resize to replace the icon buttons }
  61. procedure TCCFileMgrForm.FormResize(Sender: TObject);
  62. begin
  63.   { Send in the panel and global buttons list }
  64.   SpacePanelButtons( Panel2 );
  65. end;
  66.  
  67. { Delphi wrote this; use it to set everything up }
  68. procedure TCCFileMgrForm.FormCreate(Sender: TObject);
  69. var TheIconPanel : TFileIconPanel;
  70.     CurrentPath : String;
  71. begin
  72.   { Create the directory list box }
  73.   DirectoryListBox1 := TDirectoryListBox.Create( Panel1 );
  74.   DirectoryListBox1.Parent := Panel1;
  75.   DirectoryListBox1.Visible := true;
  76.   DirectoryListbox1.Top := 17;
  77.   DirectoryListbox1.Left := 0;
  78.   DirectoryListbox1.Height := Panel1.Height - 32;
  79.   DirectoryListbox1.Width := 0;
  80.   { Create the file list box }
  81.   FileListBox1 := TIconFileListBox.Create( Panel1 );
  82.   FileListBox1.Parent := Panel1;
  83.   FileListbox1.Top := 17;
  84.   FileListbox1.Left := 146;
  85.   FileListbox1.Height := Panel1.Height - 32;
  86.   FileListbox1.Width := 0;
  87.   FileListBox1.Visible := true;
  88.   FileListBox1.ShowGlyphs := true;
  89.   { Set intercomponent interactions }
  90.   DirectoryListBox1.FileList := FileListbox1;
  91.   DirectoryListBox1.DirLabel := Label1;
  92.   { Create the scrollbox }
  93.   ScrollBox1 := TFileIconPanelScrollBox.Create( Panel1 );
  94.   ScrollBox1.Parent := Panel1;
  95.   ScrollBox1.align := alClient;
  96.   ScrollBox1.Left := 0;
  97.   ScrollBox1.Top := 0;
  98.   ScrollBox1.Width := Panel1.Width - 32;
  99.   ScrollBox1.Height := Panel1.Height - 32;
  100.   ScrollBox1.IconsNeedRefreshing := false;
  101.   ScrollBox1.TheStoredHandle := Self.Handle;
  102.   { Get the current directory with 0 parameter }
  103.   GetDir( 0 , CurrentPath );
  104.   { Set the display caption }
  105.   Label1.Caption := CurrentPath;
  106.   { Add a trailing \ if not root }
  107.   CurrentPath := ScrollBox1.TheFWB.ForceTrailingBackSlash( CurrentPath );
  108.   { Get the icons display using scrollbox1 }
  109.   ScrollBox1.GetIconsForEntireDirectory( CurrentPath );
  110.   { Set the Icon View as default }
  111.   CurrentView := 1;
  112. end;
  113.  
  114. { Delphi wrote this; use it to close the application }
  115. procedure TCCFileMgrForm.BitBtn7Click(Sender: TObject);
  116. begin
  117.   Close;
  118. end;
  119.  
  120. { Delphi wrote this use it to perform a copy on all selected files }
  121. procedure TCCFileMgrForm.BitBtn1Click(Sender: TObject);
  122. var ThePosition  : Integer;       { Loop counter   }
  123.     CurrentName ,                 { Holds work name}
  124.     TheOldString : String;        { Holds Dir      }
  125.     finished     : Boolean;       { Loop control   }
  126.     WorkingDir   : String;        { Holds mod wd   }
  127.     TargetDir    : String;        { target of op   }
  128.     TheResult    : Integer;       { Modal res hold }
  129. begin
  130.   { Get current directory and save it for later }
  131.   GetDir( 0 , TheOldString );
  132.   { Check for need to add trailing \ }
  133.   WorkingDir := TheOldString;
  134.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDir );
  135.   { Display the Windows-based directory input form }
  136.   TheResult := DestDDForm.ShowModal;
  137.   { If not cancelled do the copy }
  138.   if TheResult = mrOK then
  139.   begin
  140.     { Get the target directory with \ OK }
  141.     TargetDir := DestDDForm.GetTargetDirectory;
  142.     { Show the hourglass to indicate waiting }
  143.     Screen.Cursor := crHourGlass;
  144.     { Setup to get all selections }
  145.     ThePosition := 1;
  146.     finished := false;
  147.     while not finished do
  148.     begin
  149.       { Call generic file getting routine based on current view}
  150.       case CurrentView of
  151.         1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  152.              ThePosition );
  153.         2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  154.              ThePosition );
  155.       end;
  156.       { If returns blank string out of selections so exit }
  157.       if CurrentName = '' then finished := true else
  158.       begin
  159.         { If a directory signal error }
  160.         if (( FileGetAttr( CurrentName ) and faDirectory ) = faDirectory ) then
  161.         begin
  162.           if MessageDlg( 'Copy Directory ' + CurrentName + ' to ' + TargetDir
  163.            +'?', mtConfirmation , mbYesNoCancel , 0 ) = mryes then
  164.           begin
  165.             ScrollBox1.TheFWB.RecursivelyCopyDirectory( CurrentName ,
  166.              TargetDir );
  167.           end;
  168.         end
  169.         else
  170.         begin
  171.           Scrollbox1.TheFWB.CopyTheFile( CurrentName , TargetDir );
  172.         end;
  173.       end;
  174.     end;
  175.     { Reset to normal cursor }
  176.     Screen.Cursor := crDefault;
  177.   end;
  178.   { Reset to the original directory }
  179.   ChDir( TheOldString );
  180. end;
  181.  
  182. { Delphi wrote this, use it to move files which are selected }
  183. procedure TCCFileMgrForm.BitBtn2Click(Sender: TObject);
  184. var ThePosition  : Integer;       { Loop counter   }
  185.     CurrentName ,                 { Holds work name}
  186.     TheOldString : String;        { Holds Dir      }
  187.     finished     : Boolean;       { Loop control   }
  188.     WorkingDir   : String;        { Holds mod wd   }
  189.     TargetDir    : String;        { target of op   }
  190.     TheResult    : Integer;       { Modal res hold }
  191. begin
  192.   { Get current directory and save it for later }
  193.   GetDir( 0 , TheOldString );
  194.   { Check for need to add trailing \ }
  195.   WorkingDir := TheOldString;
  196.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDIr );
  197.   { Display the Windows-based directory input form }
  198.   TheResult := DestDDForm.ShowModal;
  199.   { If not cancelled do the copy }
  200.   if TheResult = mrOK then
  201.   begin
  202.     { Get the target directory with \ OK }
  203.     TargetDir := DestDDForm.GetTargetDirectory;
  204.     { Show the hourglass to indicate waiting }
  205.     Screen.Cursor := crHourGlass;
  206.     { Set up to get all current selections }
  207.     ThePosition := 1;
  208.     finished := false;
  209.     while not finished do
  210.     begin
  211.       { Call generic file getting routine based on current view}
  212.       case CurrentView of
  213.         1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  214.              ThePosition );
  215.         2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  216.              ThePosition );
  217.       end;
  218.       { If returns blank string then out of selections }
  219.       if CurrentName = '' then finished := true else
  220.       begin
  221.         { If a directory signal error }
  222.         if (( FileGetAttr( CurrentName ) and faDirectory ) = faDirectory ) then
  223.         begin
  224.           if MessageDlg( 'Move Directory ' + CurrentName + ' to ' + TargetDir +
  225.            '?' , mtConfirmation , mbYesNoCancel , 0 ) = mrYes then
  226.             ScrollBox1.TheFWB.RecursivelyMoveDirectory( CurrentName ,
  227.              TargetDir );
  228.         end
  229.         else
  230.         begin
  231.           Scrollbox1.TheFWB.MoveTheFile( CurrentName , TargetDir );
  232.         end;
  233.       end;
  234.       { Reset to normal cursor }
  235.       Screen.Cursor := crDefault;
  236.     end;
  237.   end;
  238.   { Reset to the original directory }
  239.   ChDir( TheOldString );
  240.   if CurrentView = 1 then { Reset icon display if in view }
  241.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  242. end;
  243.  
  244. { Delphi wrote this, use it to delete files which are selected }
  245. procedure TCCFileMgrForm.BitBtn3Click(Sender: TObject);
  246. var ThePosition  : Integer;       { Loop counter   }
  247.     CurrentName ,                 { Holds work name}
  248.     TheOldString : String;        { Holds Dir      }
  249.     finished     : Boolean;       { Loop control   }
  250.     WorkingDir   : String;        { Holds mod wd   }
  251. begin
  252.   { Get current directory and save it for later }
  253.   GetDir( 0 , TheOldString );
  254.   { Check for need to add trailing \ }
  255.   WorkingDir := TheOldString;
  256.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDIr );
  257.   { Set up to do loop for selected files }
  258.   ThePosition := 1;
  259.   finished := false;
  260.   while not finished do
  261.   begin
  262.     { Call generic file getting routine based on current view}
  263.     case CurrentView of
  264.       1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  265.            ThePosition );
  266.       2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  267.            ThePosition );
  268.     end;
  269.     { if returns blank string out of selections }
  270.     if CurrentName = '' then finished := true else
  271.     begin
  272.       { If its a directory signal error }
  273.       if (( FileGetAttr( CurrentName ) and faDirectory ) = faDirectory ) then
  274.       begin
  275.         if MessageDlg( 'Delete Directory ' + CurrentName + '?' , mtConfirmation,
  276.          mbYesNoCancel , 0 ) = mrYes then
  277.         begin
  278.           ScrollBox1.TheFWB.RecursivelyDeleteDirectory( CurrentName );
  279.           Scrollbox1.TheFWB.RemoveDirectory( Currentname );
  280.         end;
  281.       end
  282.       else
  283.       begin
  284.         { Otherwise get confirmation message and if OKed delete file }
  285.         if MessageDlg( 'Delete file ' + CurrentName + '?', mtConfirmation ,
  286.          mbYesNoCancel , 0 ) = mrYes then
  287.         begin
  288.           ScrollBox1.TheFWB.DeleteTheFile( Currentname );
  289.         end;
  290.       end;
  291.     end;
  292.   end;
  293.   if CurrentView = 1 then { Reset icon display if in view }
  294.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  295. end;
  296.  
  297. { Delphi wrote this; use it to rename selected files }
  298. procedure TCCFileMgrForm.BitBtn4Click(Sender: TObject);
  299. var ThePosition  : Integer;       { Loop counter   }
  300.     CurrentName ,                 { Holds work name}
  301.     TheOldString : String;        { Holds Dir      }
  302.     finished     : Boolean;       { Loop control   }
  303.     WorkingDir   : String;        { Holds mod wd   }
  304. begin
  305.   { Get current directory for later }
  306.   GetDir( 0 , TheOldString );
  307.   { Check for need to add trailing \ }
  308.   WorkingDir := TheOldString;
  309.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDIr );
  310.   { Set up to do loop for selected files }
  311.   ThePosition := 1;
  312.   finished := false;
  313.   while not finished do
  314.   begin
  315.     { Call generic file getting routine based on current view}
  316.     case CurrentView of
  317.       1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  318.            ThePosition );
  319.       2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  320.            ThePosition );
  321.     end;
  322.     { If returns blank string then out of selections }
  323.     if CurrentName = '' then finished := true else
  324.     begin
  325.       { Otherwise set up the opendialog with filename and caption }
  326.       OpenDialog1.Filename := ExtractFilename( CurrentName );
  327.       { Use this to prevent error from changing directories }
  328.       OpenDialog1.Options := [ofNoChangeDir];
  329.       OpenDialog1.Title := 'Rename File';
  330.       { If not cancelled then do rename or signal error }
  331.       if OpenDialog1.Execute then
  332.       begin
  333.         ScrollBox1.TheFWB.RenameTheFile( CurrentName , OpenDialog1.Filename );
  334.       end;
  335.     end;
  336.   end;
  337.   if CurrentView = 1 then { Reset icon display if in view }
  338.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  339. end;
  340.  
  341. { Delphi wrote this; use it to make a new directory under current one }
  342. procedure TCCFileMgrForm.BitBtn5Click(Sender: TObject);
  343. begin
  344.   { Set up the dialog to get the new directory name }
  345.   OpenDialog2.FileName := '*.*';
  346.   OpenDialog2.Title := 'Enter Directory Name';
  347.   if OpenDialog1.Execute then
  348.   begin
  349.     { If not cancelled make new directory }
  350.     Scrollbox1.TheFWB.CreateNewDirectory( OpenDialog1.Filename );
  351.   end;
  352.   if CurrentView = 1 then { Reset icon display if in view }
  353.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  354. end;
  355.  
  356. { Delphi wrote this; use it to display and set data from options dialog }
  357. { Delphi wrote this; use it to synchronize the display with the listboxes }
  358. procedure TCCFileMgrForm.DirectoryListBox1Change(Sender: TObject);
  359. begin
  360.   { Set the display string to the uppercase of the current directory }
  361.   Label1.Caption := UpperCase( DirectoryListBox1.Directory );
  362. end;
  363.  
  364. { Delphi wrote this; use it to handle refreshing the scrollbox }
  365. procedure TCCFileMgrForm.FormPaint(Sender: TObject);
  366. var CurrentDirectory : String; { Get current dir }
  367. begin
  368.   { If updated, do refresh }
  369.   if ScrollBox1.IconsNeedRefreshing then
  370.   begin
  371.     Scrollbox1.Free;
  372.     ScrollBox1 := TFileIconPanelScrollBox.Create( Panel1 );
  373.     ScrollBox1.Parent := Panel1;
  374.     ScrollBox1.align := alClient;
  375.     ScrollBox1.Left := 0;
  376.     ScrollBox1.Top := 0;
  377.     ScrollBox1.Width := Panel1.Width - 32;
  378.     ScrollBox1.Height := Panel1.Height - 32;
  379.     ScrollBox1.IconsNeedRefreshing := false;
  380.     ScrollBox1.TheStoredHandle := Self.Handle;
  381.     GetDir( 0 , CurrentDirectory );
  382.     { Force trailing backslash }
  383.     CurrentDirectory := ScrollBox1.TheFWB.ForceTrailingBackSlash(
  384.      CurrentDirectory );
  385.     { Set the label to the new directory }
  386.     Label1.Caption := UpperCase( CurrentDirectory );
  387.     { Do the update }
  388.     ScrollBox1.GetIconsForEntireDirectory( CurrentDirectory );
  389.   end;
  390. end;
  391.  
  392. { Delphi wrote this; use it to respond to dbl-clicking FLB filename }
  393. procedure TCCFileMgrForm.FileListBox1DblClick(Sender: TObject);
  394. begin
  395.   { Call shellexec as a wrapper around ShellExecute API call }
  396.   { False indicates failure, signal error                    }
  397.   if not ShellExec( ExpandFileName( FileListbox1.Items[
  398.    FileListBox1.ItemIndex ] ), '' , '', false , SW_SHOWNORMAL , false ) then
  399.     MessageDlg('Could not Shell out to ' + FileListBox1.Items[
  400.      FileListBox1.ItemIndex ] , mtError, [mbOK], 0);
  401. end;
  402.  
  403. { Delphi wrote this; use it to reset the current view Icons/List }
  404. procedure TCCFileMgrForm.BitBtn8Click(Sender: TObject);
  405. var TheOldString : String; { Use to get current directory }
  406. begin
  407.   { Reset currentview }
  408.   if CurrentView = 1 then CurrentView := 2 else
  409.    CurrentView := 1;
  410.   { either show the windows boxes or the scrollbox }
  411.   case CurrentView of
  412.     1 : begin
  413.           { Make the listboxes invisible by changing align and width }
  414.           FileListBox1.Visible := false;
  415.           FileListBox1.Align := alNone;
  416.           FileListBox1.Width := 0;
  417.           DirectoryListBox1.Visible := false;
  418.           DirectoryListBox1.align := alNone;
  419.           DirectoryListBox1.Width := 0;
  420.           { Keep them from getting mouse clicks }
  421.           FileListBox1.Enabled := false;
  422.           DirectoryListBox1.Enabled := false;
  423.           { Have the scrollbox get mouse and be seen  by resetting align/wid}
  424.           Scrollbox1.align := alClient;
  425.           Scrollbox1.width := Panel1.Width - 32;
  426.           ScrollBox1.Enabled := true;
  427.           { Tell the scrollbox to repaint itself just in case }
  428.           ScrollBox1.Update;
  429.         end;
  430.     2 : begin
  431.           { Make the listboxes visible by resetting align and width}
  432.           FileListBox1.Visible := true;
  433.           DirectoryListBox1.Visible := true;
  434.           DirectoryListBox1.width := 145;
  435.           DirectoryListBox1.align := alLeft;
  436.           FileListBox1.align := alClient;
  437.           FileListBox1.Width := Panel1.Width - 32 - DirectoryListBox1.Width;
  438.           { Have them getting mouse clicks }
  439.           FileListBox1.Enabled := true;
  440.           DirectoryListBox1.Enabled := true;
  441.           { Have the scrollbox not get mouse and not be seen via align/width}
  442.           ScrollBox1.Enabled := false;
  443.           ScrollBox1.Visible := false;
  444.           ScrollBox1.align := alNone;
  445.           ScrollBox1.Width := 0;
  446.         end;
  447.   end;
  448.   Invalidate;
  449. end;
  450.  
  451. { Delphi wrote this; use it to do a file search display }
  452. procedure TCCFileMgrForm.BitBtn9Click(Sender: TObject);
  453. var CurrentDirectory : String; { Holds current directory }
  454. begin
  455.   { Set up and run the search dialog }
  456.   OpenDialog2.FileName := '*.*';
  457.   { if not cancelled do the search }
  458.   if OpenDialog2.Execute then
  459.   begin
  460.     { Get current directory }
  461.     GetDir( 0 , CurrentDirectory );
  462.     { Force trailing backslash }
  463.     CurrentDirectory :=
  464.      ScrollBox1.TheFWB.ForceTrailingBackSlash( CurrentDirectory );
  465.     { display recursive search results }
  466.     Scrollbox1.DisplayRecursiveSearchResults(
  467.      CurrentDirectory + ExtractFileName( OpenDialog2.Filename ));
  468.     Label1.Caption := 'Search Results for ' + CurrentDirectory +
  469.      ExtractFileName( OpenDialog2.FileName );
  470.   end;
  471. end;
  472.  
  473. { Delphi wrote this; use it to set a file's properties }
  474. procedure TCCFileMgrForm.BitBtn6Click(Sender: TObject);
  475. var ThePosition  : Integer;       { Loop counter   }
  476.     CurrentName ,                 { Holds work name}
  477.     TheOldString : String;        { Holds Dir      }
  478.     finished     : Boolean;       { Loop control   }
  479.     WorkingDir   : String;        { Holds mod wd   }
  480. begin
  481.   { Get current directory for later }
  482.   GetDir( 0 , TheOldString );
  483.   ThePosition := 1;
  484.   { Check for need to add trailing \ }
  485.   Workingdir := TheOldString;
  486.   WorkingDir := ScrollBox1.TheFWB.ForceTrailingBackSlash( WorkingDIr );
  487.   { Call generic file getting routine based on current view}
  488.   case CurrentView of
  489.     1 : CurrentName := ScrollBox1.GetNextSelection( WorkingDir ,
  490.          ThePosition );
  491.     2 : CurrentName := FileListBox1.GetNextSelection( WorkingDir ,
  492.          ThePosition );
  493.   end;
  494.   { If nothing selected then abort (gets ..!) }
  495.   if CurrentName = '' then
  496.   begin
  497.     MessageDlg( 'No File Selected!' , mtError , [mbOK] , 0 );
  498.     exit;
  499.   end
  500.   else
  501.   begin
  502.     { Otherwise set up the properties form and show it }
  503.     CCFMPropsForm.WorkingFileName := CurrentName;
  504.     CCFMPropsForm.ThePosition := ThePosition;
  505.     CCFMPropsForm.BitBtn4.Enabled := true;
  506.     CCFMPropsForm.Initialize;
  507.     CCFMPropsForm.ShowModal;
  508.   end;
  509.   if CurrentView = 1 then { Reset icon display if in view }
  510.    ScrollBox1.Update else FileListBox1.Update; { Otherwise update the FLB }
  511. end;
  512.  
  513. end.
  514.